package com.netty.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chengjy
 * @date 2025/4/29
 */
public class BioClient {
    public static void main(String[] args) throws IOException {
        List<Thread> threadList = new ArrayList<>();

        Thread zhangsan = new Thread(()->{
            try {
                send();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        },"张三");

        Thread lisi = new Thread(()->{
            try {
                send();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        },"李四");

        threadList.add(zhangsan);
        threadList.add(lisi);

        for (Thread thread : threadList) {
            thread.start();
        }

        // 阻塞主线程
        for (Thread thread : threadList) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }

    public static void send() throws IOException {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1",8080));
        System.out.println("连接成功~" + socket.getLocalAddress());
        OutputStream outputStream = socket.getOutputStream();
        for (int i = 0; i < 5; i++) {
            outputStream.write( (Thread.currentThread().getName() + "发送邮件： 这是第" + i + "条消息").getBytes());
            outputStream.write( ("\n").getBytes());
        }
        outputStream.close();
    }
}
